package cn.jingdalei.user.controller;

import cn.jingdalei.basic.exception.BusinessException;
import cn.jingdalei.basic.util.AjaxResult;
import cn.jingdalei.org.service.IEmployeeService;
import cn.jingdalei.user.domain.BinderDto;
import cn.jingdalei.user.domain.LoginDto;
import cn.jingdalei.user.service.ILoginService;
import cn.jingdalei.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult loginAccount(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map =  loginService.loginAccount(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("好像奔溃了");
        }
    }

    /**
     * 微信登录接口
     * @param param
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String, String> param){
        try {
            return loginService.wechat(param);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误");
        }
    }

    /**
     * 微信绑定
     * @param binderDto
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binderWechat(@RequestBody BinderDto binderDto){
        try {
            return loginService.binder(binderDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }

}
